import axios from 'axios'
import auth from '@/utils/auth.js'
import router from '@/router'
import JSONbig from 'json-bigint'

axios.defaults.baseURL = 'http://ttapi.research.itcast.cn/mp/v1_0'

axios.defaults.transformResponse = [(data) => {
    try {
        return JSONbig.parse(data)
    } catch (error) {
        return data
    }
}]

axios.interceptors.request.use(config => {
    const token = auth.getUser().token
    if (token) config.headers.Authorization = `Bearer ${token}`
    return config
}, err => Promise.reject(err))
// axios.defaults.headers.Authorization = `Bearer ${auth.getUser().token}`

axios.interceptors.response.use(res => res, err => {
    if (err.response && err.response.status === 401) {
        router.push('/login')
    }
    return Promise.reject(err)
})

export default axios